function ProductUtils() {
}

ProductUtils.getRoot = function(pages, products) {
  var root = {
    id: 0,
    parent_id: 0,
    title: '-',
    order: 0,
    _children: []
  };
  ProductUtils._getRoot(root, pages, products);
  return root;
};

ProductUtils._getRoot = function(node, pages, products) {
  if (!node._children) {
    node._children = [];
  }
  
  for (var idx in pages) {
    var page = pages[idx];
    page._products = [];
    for (var j in products) {
      var p = products[j];
      if (p.category_id == page.id) {
    	p._disabled = false;
    	p._id = p.id;
    	page._products.push(p);
      }
    }
    if (page.parent_id == node.id) {
      page._disabled = true;
      node._children[node._children.length] = page;
      ProductUtils._getRoot(page, pages, products);
    }
  }
};

ProductUtils.set_parent = function(node, stock, elm, prefix, selectable) {
    node.title = (node.name) ? node.name : node.title;
    var opt = new Option(((prefix == '') ? '' : prefix + ') ' ) + ((node.id != 0 && node.title == '') ? 'Untitled' : node.title), node._id);
	if (node._disabled || !selectable) {
      opt.disabled = true;
    }
	opt._obj = node;
    if (node.title != '-') {
      elm.options[elm.options.length] = opt;
    }
        
    if (opt.value == stock.product_id) {
	  opt.selected = 'selected';
	}
    
    for (var idx in node._children) {
      var obj = node._children[idx];
      ProductUtils.set_parent(obj, stock, elm, ((prefix == '') ? '(' : '\u2002' + prefix + '-' ) + (parseInt(idx, 10) + 1), !opt.disabled);
    }
    for (var i in node._products) {
    	var obj = node._products[i];
    	elm._obj = obj;
    	ProductUtils.set_parent(obj, stock, elm, ((prefix == '') ? '(' : '\u2002' + prefix + '-' ) + (parseInt(i, 10) + 1), 1);
    }
}

ProductUtils._get_real_categories = function(cat_ids, categories) {
  var cates = [];
  var tmps = cat_ids.split('|');
  for (var i in categories) {
	 for (var j in tmps) {
		if (tmps[j] == categories[i].id) {
		  cates.push(categories[i]);	
		}
	 }
  }
  return cates;
}

ProductUtils._if_product_in_factories = function(cat_ids, stocks, categories, products) {
	var add = true;
    var arrs = [];
    var pros = [];
    var cates = ProductUtils._get_real_categories(cat_ids, categories);

    for (var m in cates) {
       for (var n in products) {
         if (cates[m].id == products[n].category_id) {
      	   pros.push(_products[n]);
         }
       }
    }

    
    for (var j in stocks) {
  	  var s = stocks[j];
  	  if (s._deleted) {
        continue;
      }
  	  ext = false;
      for (var i in pros) {
        var p = pros[i];
        if (s.product_id == p.id) {
          ext = true;
        }
      }
      if (!ext) {
      	arrs.push(s);
      }
    }
    if (arrs.length == 0) {
       add = true;
    } else {
       add = false;
    }
    return add;
}

ProductUtils._get_total_amount = function(stocks) {
    var total1 = 0.00;
    var total2 = 0.00;
    
    for (var i in stocks) {
      var obj = stocks[i];
      if (obj._deleted) {
        continue;
      }
      if (obj.product_type == 0) {
        total1 += parseFloat(obj.total);
      } else if (obj.product_type == 1) {
      	total2 += parseFloat(obj.total);
      }
    }
    var obj = { buy: parseFloat(total1, 10).toFixed(2),
    		    sale: parseFloat(total2, 10).toFixed(2)
    }
    return obj;
}

ProductUtils._init_buy_order = function(date2, operator) {
    var order = { track_id: MyUtils.unique(),
  		            date2: date2,
  		            factory_id: 0,
  		            factory: '',
  		            supplier_id: 0,
  		            supplier_name: '',
  		            supplier_company: '',
  		            supplier_contact: '',
  		            supplier_phone: '',
  		            status: 0,
  		            handler: '',
  		            comment: '',
  		            total_amount: 0.00,
  		            operator: operator.name
    }
  	return order;
}

ProductUtils._init_sale_order = function(date2, operator) {
    var order = { track_id: MyUtils.unique(),
  		            date2: date2,
  		            factory_id: 0,
  		            factory: '',
  		            freezer_id: 0,
  	                freezer: '',
  		            customer_id: 0,
  		            customer_name: '',
  		            customer_company: '',
  		            customer_contact: '',
  		            customer_phone: '',
  		            status: 0,
  		            handler: '',
  		            comment: '',
  		            total_amount: 0.00,
  		            operator: operator.name
    }
  	return order;
  }
  
ProductUtils._init_stock = function(item, product_type, operator_type, order_type) {
  	var pro = { order_track_id: item.track_id,
  	    	      date2: item.date2,
  			        product_type: (product_type) ? product_type : 0,
  			        operator_type: (operator_type) ? operator_type : 0,
  			        order_type: (order_type) ? order_type : 0,
  			        factory_id: (item.factory_id) ? item.factory_id : 0.,
  			        factory: (item.factory) ? item.factory : '',
  			        freezer_id: (item.freezer_id) ? item.freezer_id : 0,
  			        freezer: (item.freezer) ? item.freezer : '',
  			        product_id: 0,
  			        product: '',
  			        unit: '',
  			        price: 0.00,
  			        quantity: 0,
  			        total: 0.00,
  			        comment: ''
            }
    return pro;
  }